<template>
  <section>
    <div class="filter-container">

      <common-input @keyup.enter.native="handleFilter" class="w200 ml10" placeholder="关键字"
        v-model="listQuery.keyword" />
      <el-button size="mini" class="ml10" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button size="mini" class="ml10" @click="handleCreate" type="primary" icon="el-icon-plus">新增</el-button>
    </div>
    <el-table :data="list" style="width: 100%">
      <el-table-column align="left" label="自提点名称">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="left" label="省名称">
        <template slot-scope="scope">
          <span>{{ scope.row.province }}</span>
        </template>
      </el-table-column>
      <el-table-column align="left" label="市名称">
        <template slot-scope="scope">
          <span>{{ scope.row.city }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="区县名称">
        <template slot-scope="scope">
          <span>{{ scope.row.county }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="详细地址">
        <template slot-scope="scope">
          <span>{{ scope.row.address }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="联系电话">
        <template slot-scope="scope">
          <span>{{ scope.row.phone }}</span>
        </template>
      </el-table-column>

      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-dropdown>
            <el-button type="text">操作<i class="el-icon-arrow-down el-icon--right"></i></el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handleUpdate(scope.row)">编辑</el-dropdown-item>
              <el-dropdown-item @click.native="handleDelete(scope.row)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page.sync="listQuery.pageNum" :page-sizes="[20, 30, 50]" :page-size="listQuery.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
    </div>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false"
      width="500px" @close="dialogClose">
      <el-form :model="form" :rules="rules" ref="form" label-width="150px">
        <el-form-item label="自提点名字" prop="name">
          <common-input v-model="form.name" placeholder="请输入自提点名字" />
        </el-form-item>
        <el-form-item label="区域" prop="equipmentdistrict">
          <el-cascader style="width:100%" placeholder="请选择地区" size="mini" :options="options" v-model="equipmentdistrict"
            @change="handleChange">
          </el-cascader>
        </el-form-item>

        <el-form-item label="详细地址" prop="address">
          <common-input v-model="form.address" placeholder="详细地址" />
        </el-form-item>
        <el-form-item label="经度" prop="lat">
          <common-input v-model="form.lat" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="lng">
          <common-input v-model="form.lng" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <common-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="cancel('form')">取消</el-button>
        <el-button size="mini" v-if="dialogStatus == 'create'" type="primary" @click="create('form')">确定</el-button>
        <el-button size="mini" v-else type="primary" @click="update('form')">确定</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>

import { regionData, codeToText } from 'element-china-area-data'
import { useCascaderAreaData } from '@vant/area-data';
import {
  isIntegerNotMust
} from '../../utils/validator'
import {
  pickPoint,
  pickPointUpdate,
  pickPointAdd,
  pickPointDelete
} from '../../api/product';
import {
  qa_cat_list
} from '../../api/qa_cat';
export default {
  components: {

  },
  data() {
    return {
      catList: [],
      list: null,
      listLoading: true,
      codeToTexts: codeToText,
      options: useCascaderAreaData(),
      equipmentdistrict: "",
      listQuery: {
        pageNum: 1,
        pageSize: 20,
        keyword: undefined,
        catId: undefined,

      },
      total: null,
      dialogStatus: '',
      dialogFormVisible: false,
      textMap: {
        update: '编辑地址',
        create: '新增地址'
      },
      form: {
        id: undefined,
        name: undefined,
        provinceCode: undefined,
        province: undefined,
        cityCode: undefined,
        city: undefined,
        countyCode: undefined,
        county: undefined,
        address: undefined,
        phone: undefined,
        isDefault: 0,
        lat: undefined,
        lng: undefined,
      },
      rules: {
        question: [
          {
            required: true,
            message: '请输入问题',
            trigger: 'blur'
          }
        ],


        orderNum: [
          {
            validator: isIntegerNotMust,
            trigger: 'blur'
          }
        ]
      },
    }
  },
  mounted() {
  },
  created() {

    this.getList();
  },

  methods: {
    handleChange(value) {
      console.log('value', value, this.options)
      // const item = undefined
      const item2 = {}

      let item = this.options.find(it => it.value == value[0])

      if (item) {
        this.form.province = item.label

        let item2 = item.children.find(its => its.value == value[1])

        if (item2) {
          this.form.city = item2.label
          let item3 = item2.children.find(itss => itss.value == value[2])

          this.form.county = item3.label
        }

        console.log(this.form)

      }

      // if (item) {

      // this.form.city = item2.label
      // // }
      // // if (item2) {
      //
      // // }


      // console.log(item, item2)



      this.form.cityCode = value[1]
      this.form.provinceCode = value[0]
      this.form.countyCode = value[2]
    },
    getList() {
      this.listLoading = true;
      pickPoint(this.listQuery).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.listLoading = false;
      })
    },
    handleCreate() {
      this.resetTemp();
      this.dialogStatus = 'create';
      this.dialogFormVisible = true;
    },
    resetTemp() {
      this.form = {
        id: undefined,
        name: undefined,
        provinceCode: undefined,
        province: undefined,
        cityCode: undefined,
        city: undefined,
        countyCode: undefined,
        county: undefined,
        address: undefined,
        phone: undefined,
        isDefault: 0,
        lat: undefined,
        lng: undefined,
      };
    },
    create(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          // if (!this.form.orderNum) {
          //   this.form.orderNum = 0;
          // }
          pickPointAdd(this.form)
            .then(() => {
              this.dialogFormVisible = false;
              this.getList();
              this.$message.success("问题创建成功");
            })
        } else {
          return false;
        }
      });
    },
    update(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          // if (!this.form.orderNum) {
          //   this.form.orderNum = 0;
          // }
          this.dialogFormVisible = false;
          this.form.password = undefined;
          pickPointUpdate(this.form).then(() => {
            this.dialogFormVisible = false;
            this.getList();
            this.$message.success("更新成功");
          });
        } else {
          return false;
        }
      });
    },
    cancel(formName) {
      this.dialogFormVisible = false;
      this.$refs[formName].resetFields();
    },
    handleUpdate(row) {
      // qa_detail(row.id)
      //   .then(response => {
      this.form = row;
      this.dialogFormVisible = true;
      this.dialogStatus = 'update';
      // });
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {

          pickPointDelete(row.id).then(() => {
            this.$message.success("问题删除成功");
            this.getList()

          }).catch(() => {
            this.$message.success("删除失败");
          });
        }).catch(() => {

        });
    },
    dialogClose() {
      this.$refs.form.resetFields();
    },
    handleFilter() {
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    }
  }
};
</script>
